package com.eedi.framework.logger.dal.mapper;

import com.eedi.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.logger.controller.sys.vo.operatelog.SysOperateLogPageReq;
import com.eedi.framework.logger.controller.sys.vo.operatelog.SysOperateLogResp;
import com.eedi.framework.logger.convert.SysOperateLogConvert;
import com.eedi.framework.logger.enmus.SysLogTypeEnum;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.logger.dal.dataobject.SysLogOperateDO;
import com.eedi.framework.tenant.core.db.TenantBaseDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;

@Mapper
public interface SysOperateLogMapper extends BaseMapperX<SysLogOperateDO> {

    default PageResult<SysOperateLogResp> selectPage(SysOperateLogPageReq reqVO, Collection<String> userIds, SysLogTypeEnum sysLogType, String sysTenantId) {
        LambdaQueryWrapperX<SysLogOperateDO> query = new LambdaQueryWrapperX<SysLogOperateDO>()
                .likeIfPresent(SysLogOperateDO::getModule, reqVO.getModule())
                .inIfPresent(SysLogOperateDO::getUserId, userIds)
                .eqIfPresent(SysLogOperateDO::getOptType, reqVO.getOptType())
                .eqIfPresent(SysLogOperateDO::getUserType,reqVO.getUserType())
                .eqIfPresent(TenantBaseDO::getSysTenantId,sysTenantId)
                .eqIfPresent(SysLogOperateDO::getUserType, reqVO.getUserType())
                .betweenIfPresent(SysLogOperateDO::getStartTime, reqVO.getStartTime(),reqVO.getEndTime());
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(SysLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(SysLogOperateDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }
        // 降序
        query.orderByDesc(SysLogOperateDO::getSysOperateLogId);
        PageResult<SysLogOperateDO> result = selectPage(reqVO, query);
        return result.convertPage( SysOperateLogConvert.INSTANCE::convert);
    }

}
